package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.dto.LikeDto;
import com.facebank.usersupport.dto.NotesDto;
import com.facebank.usersupport.dto.NotesLikesDto;
import com.facebank.usersupport.mapper.usersupport.usersupport.*;
import com.facebank.usersupport.model.*;
import com.facebank.usersupport.service.INotesService;
import com.facebank.usersupport.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class NotesServiceImpl implements INotesService {
    @Autowired
    private DgutAlbumMapper dgutAlbumMapper;
    @Autowired
    private DgutCommentMapper dgutCommentMapper;
    @Autowired
    private DgutNotesMapper dgutNotesMapper;
    @Autowired
    private DgutLikeMapper dgutLikeMapper;
    @Autowired
    private AssociatorMapper associatorMapper;

    @Override
    public RestModel add(String content, HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        DgutNotes notes = new DgutNotes();
        notes.setAssociator(assId);
        notes.setTitle("未编辑");
        notes.setContent(content);
        notes.setGtmCreate(System.currentTimeMillis());
        int is_success = dgutNotesMapper.insertSelective(notes);
        if (is_success!=1){return new RestModel();}
        Long notesId = notes.getId();
        String path = "http://yaozun.fun:8080/images/upload/";
        //图片一
        DgutAlbum dgutAlbum = new DgutAlbum();
        dgutAlbum.setNotesId(notesId);
        dgutAlbum.setImageUrl(path+"travel_1.jpg");
        dgutAlbumMapper.insertSelective(dgutAlbum);
        //图片二
        DgutAlbum dgutAlbum2 = new DgutAlbum();
        dgutAlbum2.setNotesId(notesId);
        dgutAlbum2.setImageUrl(path+"travel_2.jpg");
        dgutAlbumMapper.insertSelective(dgutAlbum2);
        //图片一
        DgutAlbum dgutAlbum3 = new DgutAlbum();
        dgutAlbum3.setNotesId(notesId);
        dgutAlbum3.setImageUrl(path+"travel_3.jpg");
        dgutAlbumMapper.insertSelective(dgutAlbum3);
        //图片一
        DgutAlbum dgutAlbum4 = new DgutAlbum();
        dgutAlbum4.setNotesId(notesId);
        dgutAlbum4.setImageUrl(path+"travel_4.jpg");
        dgutAlbumMapper.insertSelective(dgutAlbum4);

        return new RestModel("新增成功");
    }

    @Override
    public RestModel show() {
        List<NotesDto> notes = dgutNotesMapper.selectNotes();
        List<NotesLikesDto> notesLikes = new ArrayList<>();
        for (NotesDto i:notes) {
            List<LikeDto> likeDtos = i.getLikes();
            Integer likes = likeDtos.size();
            NotesLikesDto notesLike = new NotesLikesDto();
            notesLike.setId(i.getId());
            Associator associator = associatorMapper.selectByPrimaryKey(i.getAssociator());
            notesLike.setAssociator(associator.getAssociatorName());
            notesLike.setContent(i.getContent());
            notesLike.setLikes(likes);
            notesLike.setStatus(i.getStatus());
            notesLike.setTitle(i.getTitle());
            notesLikes.add(notesLike);
            notesLike.setAlbums(i.getAlbums());
            notesLike.setGtmCreate(i.getGtmCreate());
        }
        return new RestModel(notesLikes);
    }

    @Override
    public RestModel person(Long assId) {
        List<NotesDto> notes = dgutNotesMapper.selectNotesByAssId(assId);
        List<NotesLikesDto> notesLikes = new ArrayList<>();
        for (NotesDto i:notes) {
            List<LikeDto> likeDtos = i.getLikes();
            Integer likes = likeDtos.size();
            NotesLikesDto notesLike = new NotesLikesDto();
            notesLike.setId(i.getId());
            Associator associator = associatorMapper.selectByPrimaryKey(i.getAssociator());
            notesLike.setAssociator(associator.getAssociatorName());
            notesLike.setContent(i.getContent());
            notesLike.setLikes(likes);
            notesLike.setStatus(i.getStatus());
            notesLike.setTitle(i.getTitle());
            notesLikes.add(notesLike);
            notesLike.setAlbums(i.getAlbums());
            notesLike.setGtmCreate(i.getGtmCreate());
        }
        return new RestModel(notesLikes);
    }

    @Override
    public RestModel detail(Long notesId) {
        NotesDto notes = dgutNotesMapper.selectNoteByNotesId(notesId);
        NotesLikesDto notesLikes = new NotesLikesDto();
        List<LikeDto> likeDtos = notes.getLikes();
        Integer likes = likeDtos.size();
        notesLikes.setId(notes.getId());
        Associator associator = associatorMapper.selectByPrimaryKey(notes.getAssociator());
        notesLikes.setAssociator(associator.getAssociatorName());
        notesLikes.setContent(notes.getContent());
        notesLikes.setLikes(likes);
        notesLikes.setStatus(notes.getStatus());
        notesLikes.setTitle(notes.getTitle());
        notesLikes.setAlbums(notes.getAlbums());
        notesLikes.setGtmCreate(notes.getGtmCreate());

        return new RestModel(notesLikes);
    }

    @Override
    public RestModel addComment(DgutComment comment) {
        comment.setCommentTime(System.currentTimeMillis());
        int is_success = dgutCommentMapper.insertSelective(comment);
        return new RestModel("成功");
    }

    @Override
    public RestModel showComment(Long notesId) { return new RestModel(dgutCommentMapper.selectByNotesId(notesId)); }

    @Override
    public RestModel like(Long notesId, HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        DgutLike dgutLike = dgutLikeMapper.selectBynotesIdAssId(notesId,assId);
        if (dgutLike==null){
            DgutLike dgutLike1 = new DgutLike();
            dgutLike1.setAssociatorId(assId);
            dgutLike1.setNotesId(notesId);
            dgutLikeMapper.insertSelective(dgutLike1);
            return new RestModel("点赞");
        }
        if (dgutLike!=null){
            Long id = dgutLike.getId();
            dgutLikeMapper.deleteByPrimaryKey(id);
            return new RestModel("取消点赞");
        }
        return new RestModel("205","失败");
    }
}
